Imports KanBan.Pgd.Presentation.Helpers
Imports KanBan.Pgd.Common
Imports KanBan.Pgd.Domain
Imports Iesi.Collections.Generic
Imports KanBan.Pgd.Domain.Entities
Imports KanBan.Pgd.Presentation.Controllers
Imports KanBan.Pgd.Presentation.Views
Imports KanBan.Pgd.Infrastructure.Repositories

Public Class EditPerformanceEvaluation


    Private Const DefaulGridViewRowHeight As Integer = 36

    Public Sub New(ByVal id As Guid)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Me.PerformanceEvaluationId = id

        Dim per As New PerformanceEvaluationRepository()
        Dim pe As PerformanceEvaluation = per.GetById(id)

        Me.AddGoals(pe.GoalRatings)
        Me.Comments = pe.Comments

    End Sub

    Public Sub AddGoals(ByVal ratings As IEnumerable(Of GoalRating))

        For Each gr As GoalRating In ratings
            AddGoal(gr)
        Next

    End Sub

    Private _performanceEvaluationId As Guid
    Public Property PerformanceEvaluationId() As Guid
        Get
            Return _performanceEvaluationId
        End Get
        Set(ByVal value As Guid)
            _performanceEvaluationId = value
        End Set
    End Property

    Public Property Comments() As String
        Get
            Return txtComments.Text
        End Get
        Set(ByVal value As String)
            txtComments.Text = value
        End Set
    End Property

    Public Sub UpdateGoalRating(ByVal goalRating As GoalRating)

        For Each r As DataGridViewRow In grvGoals.Rows
            If CType(r.Cells(GridColumns.GoalId).Value, Guid) = goalRating.Goal.Id Then
                r.Cells(GridColumns.Name).Value = goalRating.Goal.Name
                r.Cells(GridColumns.Rating).Value = goalRating.RatingLevel
                Exit For
            End If
        Next

    End Sub

    Private Sub AddGoal(ByVal rating As GoalRating)

        Dim row As New DataGridViewRow()
        row.CreateCells(grvGoals)
        row.Height = DefaulGridViewRowHeight

        row.Cells(GridColumns.GoalId).Value = rating.Goal.Id
        row.Cells(GridColumns.Name).Value = rating.Goal.Name
        row.Cells(GridColumns.Rating).Value = rating.RatingLevel

        grvGoals.Rows.Add(row)

    End Sub

    Private Enum GridColumns
        Image = 0
        GoalId = 1
        Name = 2
        Rating = 3
        Edit = 4
    End Enum

    Private Sub grvGoals_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grvGoals.CellClick

        If e.RowIndex >= 0 Then

            Dim id As Guid = CType(grvGoals.Rows(e.RowIndex).Cells(GridColumns.GoalId).Value, Guid)

            If e.ColumnIndex = GridColumns.Edit Then
                Me.EditGoalRating(id)
            End If

        End If

    End Sub

    Public Sub EditGoalRating(ByVal id As Guid)


        Dim form As New EditGoalRating(Me.PerformanceEvaluationId, id)
        AddHandler form.GoalRatingUpdated, AddressOf OnGoalRatingUpdated

        form.ShowDialog()


    End Sub

    Private Sub OnGoalRatingUpdated(ByVal goalrating As GoalRating)

        Me.UpdateGoalRating(goalrating)


    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Me.SaveOrUpdate()
        Me.Close()
    End Sub

    Public Sub SaveOrUpdate()


        Dim per As New PerformanceEvaluationRepository()
        Dim pe As PerformanceEvaluation = per.GetById(Me.PerformanceEvaluationId)

        If pe IsNot Nothing Then

            pe.Comments = Me.Comments
            per.SaveOrUpdate(pe)

        End If

    End Sub

End Class